package cn.myfirefly.moming.portal.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.portal.model.FunctionActionDto;
import cn.myfirefly.moming.portal.model.FunctionActionFilterDto;
import cn.myfirefly.moming.portal.model.FunctionDto;
import cn.myfirefly.moming.portal.model.FunctionFilterDto;
import cn.myfirefly.moming.portal.service.FunctionActionService;
import cn.myfirefly.moming.portal.service.FunctionService;
import cn.myfirefly.moming.portal.service.SystemAccessService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;

@Controller
@RequestMapping("function")
public class FunctionController extends BaseController {

	@Autowired
	private SystemAccessService systemAccessService;
	
	@Autowired
	private FunctionService functionService;
	
	@Autowired
	private FunctionActionService functionActionService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("systemAccessList", 
				systemAccessService.getSystemAccessList('1'));
		return "function/index";
	}
	
	@RequestMapping("fun_index.do")
	public String funIndex(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("systemId", request.getParameter("systemId"));
		return "function/list";
	}
	
	@RequestMapping("action/index.do")
	public String indexAction(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("funSn", request.getParameter("funSn"));
		request.setAttribute("systemId", request.getParameter("systemId"));
		return "function/action/index";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "function/edit";
	}
	
	@RequestMapping("action/edit.do")
	public String editAction(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "function/action/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "function/edit";
	}
	
	@RequestMapping("action/add.do")
	public String addAction(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "function/action/edit";
	}
	
	@RequestMapping("save.do")
	@ResponseBody
	public ResultDto save(FunctionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = functionService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@RequestMapping("action/save.do")
	@ResponseBody
	public ResultDto saveAction(FunctionActionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
//		if (StrUtil.isBlank(dto.getStatus())) {
//			dto.setStatus("R");
//		}
		warpModifyInfo(dto, request);
		String msg = functionActionService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto list(FunctionFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String systemId = request.getParameter("systemId");
		PageResultDto result = functionService.getList(dto, new Integer(systemId));
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("action/list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto listAction(FunctionActionFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String funSn = request.getParameter("funSn");
		PageResultDto result = functionActionService.getList(dto, Integer.valueOf(funSn));
		return result;
	}
	
	@RequestMapping("getFunc.do")
	@ResponseBody
	public ResultDto getFunction(HttpServletRequest request, 
			HttpServletResponse response) {
		String functionSn = request.getParameter("funSn");
		FunctionDto function = functionService.getFunction(Integer.valueOf(functionSn));
		List<FunctionDto> list = new ArrayList<FunctionDto>();
		list.add(function);
		return success(list);
	}
	
	@RequestMapping("action/getAction.do")
	@ResponseBody
	public ResultDto getFunctionAction(HttpServletRequest request, 
			HttpServletResponse response) {
		String funActionSn = request.getParameter("funActionSn");
		FunctionActionDto funAction = functionActionService.getFunctionAction(Integer.valueOf(funActionSn));
		return success(funAction);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(FunctionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = functionService.del(dto.getFunSn());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	@RequestMapping("action/del.do")
	@ResponseBody
	public ResultDto delAction(FunctionActionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = functionActionService.del(dto.getFunActionSn());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
}
